package dnuifood_cjy.dnuifood_16110100529.controller;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import dnuifood_cjy.dnuifood_16110100529.Adapter.FoodInfoAdapter;
import dnuifood_cjy.dnuifood_16110100529.R;
import dnuifood_cjy.dnuifood_16110100529.bean.CollectStatus;
import dnuifood_cjy.dnuifood_16110100529.bean.CommentInfo;
import dnuifood_cjy.dnuifood_16110100529.bean.FoodInfo;
import dnuifood_cjy.dnuifood_16110100529.bean.ResultBean;
import dnuifood_cjy.dnuifood_16110100529.listener.OnRetrofitListener;
import dnuifood_cjy.dnuifood_16110100529.model.Constants;
import dnuifood_cjy.dnuifood_16110100529.model.FoodModel;
import com.squareup.picasso.Picasso;

import java.util.List;

public class FoodInfoActivity extends BaseActivity implements OnRetrofitListener {
    private Context context;
    private String food_id,user_id;
    private  String shop_name="",result="";
    private String food_foodname="",phonenum="";
    private TextView foodinfo_price;
    private TextView foodinfo_intro;
    private TextView foodname;
    private ImageView foodinfo_return,foodinfo_purchase,collect,foodinfo_pic;
    private Button foodinfo_call;
    private List<CommentInfo> list;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private FoodInfoAdapter adapter;
    @Override
    void initViews() {
        setLayout(R.layout.activity_food_info);
        foodinfo_pic=findViewById(R.id.imageView9);
        foodname=findViewById(R.id.textView13);
        foodinfo_price=findViewById(R.id.textView15);
        foodinfo_intro=findViewById(R.id.textView14);
        foodinfo_return=findViewById(R.id.imageView8);
        foodinfo_purchase=findViewById(R.id.imageView10);
        collect=findViewById(R.id.imageView12);
        foodinfo_call=findViewById(R.id.button6);

        recyclerView = findViewById(R.id.recyclerview);

        context= FoodInfoActivity.this;
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);
    }

    @Override
    void initEvents() {
        //处理【返回】、【收藏】、【立即购买】和【联系商家】按钮的点击事件
        // 【返回】按钮调用 finish()关闭当前界面
        // 【收藏】按钮调用 collect()方法
        // 立即购买】按钮打开购买界面
        //【联系商家】按钮打开打电话界面
//        shop_name=getIntent().getStringExtra("shop_name");
//        shop_id=getIntent().getStringExtra("shop_id");
        //   shop_id=getIntent().getStringExtra("shop_id");
        food_id=getIntent().getStringExtra("food_id");
        food_foodname=getIntent().getStringExtra("foodname");
        foodname.setText(food_foodname);
        phonenum = getIntent().getStringExtra("phonenum");
        foodinfo_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); }
        });

        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            } });

        foodinfo_purchase.setOnClickListener(new View.OnClickListener() { @Override
        public void onClick(View v) {
            Intent intent = new Intent(FoodInfoActivity.this,PurchaseActivity.class);

            startActivity(intent);
        } });
        foodinfo_call.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.M) @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("tel:" + phonenum);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            if (checkSelfPermission(Manifest.permission.CALL_PHONE) !=

                    PackageManager.PERMISSION_GRANTED) {
                startActivity(intent);
                return; }
        } });
    }

    @Override
    void initData() {


        //通过 Intent 获取菜谱 id
//获取菜谱的详细信息，调用 showFoodInfo()方法
//获取菜谱的评论列表，调用 showComments()方法
// 获取菜谱的收藏状态，调用 iscollected()方法

        showFoodInfo();
        showComments();
        iscollected();
    }

    //获取菜谱的详细信息
    private void showFoodInfo() {
        FoodModel  foodModel=new FoodModel();
        foodModel.getFoodById(food_id,this);
    } //获取菜谱的评论列表
    private void showComments(){
        FoodModel foodModel=new FoodModel();

        foodModel.getAllCommentsByFood(food_id,this);

    }
    //判断菜谱收藏状态
    private void iscollected(){
        FoodModel foodModel=new FoodModel();
        foodModel.isCollected(getUser_id(), food_id,"1",this);
    }
    // 对菜谱的收藏信息进行修改
    private void collect() {
        FoodModel foodmodel= new FoodModel();
        foodmodel.userCollectFood(getUser_id(), food_id, this);
    }


    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag)
        {
            case 1:
                FoodInfo foodinfo = (FoodInfo) o;

                if(!foodinfo.getPic().equals("")){
                    Picasso.with(context).load(Constants.BASE_URL+foodinfo.getPic()).into(foodinfo_pic); }
                foodinfo_intro.setText("简介:"+foodinfo.getIntro()); foodinfo_price.setText("¥"+foodinfo.getPrice()+"元");
                break;
            case 2:
                adapter = new FoodInfoAdapter(FoodInfoActivity.this, (List<CommentInfo>) o,R.layout.shop_item3);
                recyclerView.setAdapter(adapter);

                break;
            case 3:
                result=((CollectStatus)o).getCollected().toString();
                if (result.equals("0")) {
                    collect.setImageResource(R.drawable.ic_collection);
                }
                else {
                    collect.setImageResource(R.drawable.ic_collection_fill);
                }
                break;


            case 4:
                if (((ResultBean)o).getSuccess().equals("1")) {

                    if (result.equals("0")) {
                        collect.setImageResource(R.drawable.ic_collection_fill);
                        Toast.makeText(FoodInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        collect.setImageResource(R.drawable.ic_collection);
                        Toast.makeText(FoodInfoActivity.this, "取消收藏成功",
                                Toast.LENGTH_SHORT).show();
                        result = "0";
                    }
                } else {
                    Toast.makeText(FoodInfoActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(FoodInfoActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();
    }

}
